package com.ld.security.server.admin.modules.ge.service.impl;

import com.ld.security.common.core.base.column.ListString;
import com.ld.security.common.core.exception.ApiException;
import com.ld.security.common.core.util.ReflectionUtil;
import com.ld.security.common.core.util.StringUtil;
import com.ld.security.common.mybatis.base.entity.TreeEntity;
import com.ld.security.common.mybatis.base.service.ISuperServiceImpl;
import com.ld.security.server.admin.modules.ge.constant.GeFieldConstant;
import com.ld.security.server.admin.modules.ge.entity.*;
import com.ld.security.server.admin.modules.ge.mapping.IGeModelMapping;
import com.ld.security.server.admin.modules.ge.mapping.ProxyMapping;
import com.ld.security.server.admin.modules.ge.po.*;
import com.ld.security.server.admin.modules.ge.service.*;
import com.ld.security.server.admin.modules.ge.vo.EarlyDetailsVo;
import com.ld.security.server.admin.modules.ge.vo.GeModelArgsVo;
import com.ld.security.server.admin.modules.ge.vo.ModelDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Transactional
class GeModelServiceImpl extends ISuperServiceImpl<GeModelEntity> implements
        IGeModelService {

    @Autowired
    IGeTableService iGeTableService;
    @Autowired
    IGeFieldService iGeFieldService;

    @Autowired
    IGeModelMapping iGeModelMapping;

    @Autowired
    ProxyMapping proxyMapping;

    @Autowired
    IGeColumnService iGeColumnService;

    @Autowired
    IGeLinkService iGeLinkService;


    @Autowired
    IGeEarlyModelService iGeEarlyModelService;


    @Override
    public GeModelArgsVo getGeModelArgsVo(GeModelArgsPo po) {
        GeModelArgsVo vo = new GeModelArgsVo();
        vo.setProjectId(po.getProjectId());
        vo.setViewId(po.getViewId());
        vo.setDataSourceId(po.getDataSourceId());
        this.proxyQuery(vo);
        return vo;
    }

    @Override
    public ModelDetailVo getModelDetail(String id) {
        ModelDetailVo vo = new ModelDetailVo();
        vo.setId(id);
        this.proxyQuery(vo);

        EarlyDetailsVo earlyDetailsVo = iGeEarlyModelService.getEarlyDetails(id);

        vo.setEarlyModels(earlyDetailsVo.getEarlyModels());

        return vo;
    }

    @Override
    public int deleteModel(String id) {
        ModelDetailVo vo = new ModelDetailVo();
        vo.setId(id);
        this.proxyQuery(vo);
        this.proxyRemove(vo);


        //预警模型删除
        EarlyDetailsVo earlyDetailsVo = iGeEarlyModelService.getEarlyDetails(id);

        this.proxyRemove(earlyDetailsVo);


        return 1;
    }

    @Override
    public ModelDetailVo modelClonePo(ModelClonePo po) {


        ModelDetailVo vo = new ModelDetailVo();
        vo.setId(po.getId());
        this.proxyQuery(vo);


        vo.getModel().setCode(po.getCode());
        vo.getModel().setId(null);
        this.save(vo.getModel());
        for (GeFieldEntity field : vo.getFields()) {
            field.setId(null);
            field.setModelId(vo.getModel().getId());
            iGeFieldService.save(field);
        }
        return vo;
    }




    @Override
    public void saveModel(SaveModelPo po) {


        //历史数据删除
        this.deleteModel(po.getModel().getId());

        this.proxySave(po);

        //预警模型更新
        for (EarlyDetailsVo.EarlyDetails earlyDetails : po.getEarlyModels()) {
            earlyDetails.getEarlyModel().setModelId(po.getModel().getId());
            this.proxySave(earlyDetails);
        }
    }

}
